<template>
  <div>
    <div class="project" v-if="!projectAddShow && !followAddShow">
      <!-- 筛选按钮 -->
      <div class="search-button">
        <el-button
          v-hasPermi="['system:workList:query']"
          class="btn search"
          @click="getProjectList"
          ><el-icon class="icon" color="#fff"> <Search /> </el-icon
          >查询</el-button
        >
        <el-button class="btn add" @click="handleRefresh"
          ><el-icon class="icon" color="#fff"> <Refresh /> </el-icon
          >重置</el-button
        >
      </div>
      <!-- 筛选条件 -->
      <div class="search">
        <div class="search-item">
          <el-input
            clearable
            v-model="params.projectName"
            class="search-input"
            placeholder="请输入项目名称"
          ></el-input>
        </div>
        <div class="search-item">
          <el-input
            clearable
            v-model="params.workOrderName"
            class="search-input"
            placeholder="请输入工单名称"
          ></el-input>
        </div>
        <div class="search-item">
          <el-input
            clearable
            v-model="params.applicantName"
            class="search-input"
            placeholder="请输入申请人名称"
          ></el-input>
        </div>
        <div class="search-item">
          <el-select
            v-model="params.workOrderType"
            placeholder="请选择工单类型"
            clearable
            class="search-input"
          >
            <el-option
              v-for="dict in typeList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </div>
        <div class="search-item">
          <el-select
            v-model="params.constructionStatus"
            placeholder="请选择施工状态"
            clearable
            class="search-input"
          >
            <el-option
              v-for="dict in statusList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </div>
      </div>

      <!-- 表格数据 -->
      <el-table
        v-loading="loading"
        :data="projectList"
        border
        height="64vh"
        :header-cell-style="{ 'text-align': 'center' }"
      >
        <!-- 操作模式 -->
        <el-table-column
          label="操作"
          :show-overflow-tooltip="true"
          :width="'140px'"
        >
          <template #default="{ row }">
            <div class="table-operate">
              <el-icon
                v-hasPermi="['system:workList:detail']"
                title="查看详情"
                color="#007CEF"
                class="table-icon"
                :size="20"
                @click="handleOperate('detail', row)"
              >
                <Document />
              </el-icon>
              <!-- ,'已退回' -->
              <el-icon
                v-if="['已分配', '进行中'].includes(row.constructionStatusName) &&row.auditStatus == 1"
                v-hasPermi="['system:workList:execute']"
                title="执行"
                color="#007CEF"
                class="table-icon"
                :size="20"
                @click="handleOperate('execute', row)"
              >
              <img
                    src="@/assets/icons/images/execute.png"
                    style="width: 18px; height: 18px"
                  />
              </el-icon>

              <el-icon
                v-hasPermi="['system:workList:remove']"
                title="删除"
                color="#E33838"
                class="table-icon"
                :size="20"
                @click="handleOperate('delete', row)"
              >
                <Delete />
              </el-icon>
            </div> </template
        ></el-table-column>
        <el-table-column
          type="index"
          label="序号"
          :index="indexMethod"
          width="70"
          align="center"
        />
        <el-table-column
          label="项目名称"
          prop="projectName"
          :show-overflow-tooltip="true"
          width="120"
        >
        <template #default="{ row }">
            <span>{{ row.projectName ? row.projectName : "--" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="工单名称"
          prop="workOrderName"
          :show-overflow-tooltip="true"
          width="120"
        />
        <el-table-column
          label="审核状态"
          prop="approvalStatusName"
          :show-overflow-tooltip="true"
          width="100"
        >
          <template #default="{ row }">
            <span :style="getStatusColor(row.approvalStatusName)">{{
              row.approvalStatusName
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="施工状态"
          prop="constructionStatusName"
          :show-overflow-tooltip="true"
          width="100"
        />
        <el-table-column
          label="申请人"
          prop="applicantName"
          :show-overflow-tooltip="true"
          width="100"
          >
        <template #default="{ row }">
            <span>{{ row.applicantName ? row.applicantName : "--" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          :show-overflow-tooltip="true"
          width="160"
        />
        <el-table-column
          label="业务人员"
          prop="userName"
          :show-overflow-tooltip="true"
          width="100"
          >
        <template #default="{ row }">
            <span>{{ row.userName ? row.userName : "--" }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column
          label="施工完结时间"
          prop="finishTime"
          :show-overflow-tooltip="true"
          width="160"
        /> -->

        <el-table-column
          label="施工省市区"
          :show-overflow-tooltip="true"
          width="200"
        >
          <template #default="{ row }">
            {{ row.provinceName ? row.provinceName+row.cityName+row.areaName : "--" }}
          </template>
        </el-table-column>
        <el-table-column
          label="详细地址"
          prop="address"
          :show-overflow-tooltip="true"
          width="150"
          >
        <template #default="{ row }">
            <span>{{ row.address ? row.address : "--" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="工单类型"
          prop="workOrderTypeName"
          :show-overflow-tooltip="true"
          width="100"
        />
        <el-table-column
          label="计划开始时间"
          prop="desireStartTime"
          :show-overflow-tooltip="true"
          width="160"
          >
        <template #default="{ row }">
            <span>{{ row.desireStartTime ? row.desireStartTime : "--" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="计划结束时间"
          prop="desireEndTime"
          :show-overflow-tooltip="true"
          width="160"
          >
        <template #default="{ row }">
            <span>{{ row.desireEndTime ? row.desireEndTime : "--" }}</span>
          </template>
        </el-table-column>
     
      </el-table>

      <!-- 分页 -->
      <pagination
        class="pagination"
        :total="total"
        v-model:page="params.pageNum"
        v-model:limit="params.pageSize"
        @pagination="getProjectList"
      />
    </div>
    <ProjectAdd
      v-if="projectAddShow"
      ref="projectAddRef"
      v-model:projectAddShow="projectAddShow"
      @updateFollowAddShow="followAddShow = false"
      @refreshList="getProjectList"
      :pageType="pageType"
    />
    <FollowAdd
      ref="followAddRef"
      v-model:followAddShow="followAddShow"
      @refreshList="getProjectList"
      :pageType="pageType"
      v-if="followAddShow"
    />
  </div>
</template>

<script setup>
import FollowAdd from "./followAdd.vue";
import ProjectAdd from "../workApply/projectAdd";
import { getProjectListApi, deleteProjectApi } from "@/api/sales/project";
import {
  deleteWorkApplyApi,
  getWorkCheckListApi,
} from "@/api/operation/work";
import { getDictsApi } from "@/api/dict";
import { ElMessage, ElMessageBox } from "element-plus";
// 路由
const route = useRoute();
// 搜索参数
const params = reactive({
  applicantName: "", // 申请人
  approvalStatus: null, // 审批状态
  constructionStatus: null, // 	施工状态
  projectName: "", // 项目名称
  workOrderName: "", // 工单名称
  workOrderType: null, // 工单类型
  pageNum: 1,
  pageSize: 50,
});
// 表格加载效果
const loading = ref(false);
// 项目新增组件显隐
const projectAddShow = ref(false);
// 跟进新增组件显隐
const followAddShow = ref(false);
// 项目列表
const projectList = ref([]);
// 总数
const total = ref(0);
// 项目新增组件引用
const projectAddRef = ref(null);
// 跟进组件引用
const followAddRef = ref(null);
// 操作类型
const pageType = ref("");
// 审核状态列表
const checkList = ref([]);
// 是否签合同列表
const contractList = ref([]);
// 项目状态列表
const statusList = ref([]);
// 项目类型列表
const typeList = ref([]);

/**
 * @description: 重置
 */
 const handleRefresh = () => {
  Object.assign(params, {
    applicantName: "", // 申请人
  approvalStatus: null, // 审批状态
  constructionStatus: null, // 	施工状态
  projectName: "", // 项目名称
  workOrderName: "", // 工单名称
  workOrderType: null, // 工单类型
    pageNum: 1,
    pageSize: 50,
  });
  getProjectList()
};

/**
 * @description: 获取项目列表
 */
const getProjectList = async () => {
  try {
    loading.value = true;
    let { pageNum, pageSize } = params;
    const res = await getWorkListApi({ pageNum, pageSize }, params);

    projectList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  } catch {
    loading.value = false;
  }
};

/**
 * @description: 表格操作
 * @param {String} type 类型
 * @param {Object} row  当前行数据
 */
const handleOperate = async (type, row) => {
  switch (type) {
    case "edit":
      pageType.value = type;
      projectAddShow.value = true;
      nextTick(() => {
        projectAddRef.value.handleDetailInfo(row.workOrderId);
      });
      break;
    case "check":
      pageType.value = type;
      projectAddShow.value = true;
      nextTick(() => {
        projectAddRef.value.handleDetailInfo(row.workOrderId);
      });
      break;
    case "detail":
      pageType.value = type;
      projectAddShow.value = true;
      nextTick(() => {
        projectAddRef.value.handleDetailInfo(row.workOrderId);
      });
      break;
    case "execute":
      pageType.value = type;
      followAddShow.value = true;
      nextTick(() => {
        followAddRef.value.handleDetailInfo(row);
      });
      break;
    case "delete":
      ElMessageBox.confirm("此操作将删除该工单，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        confirmButtonClass: "confirmBtnClass",
        cancelButtonClass: "cancelBtnClass",
        type: "warning",
      })
        .then(async () => {
          const res = await deleteWorkApplyApi(row.workOrderId);
          if (res.code === 200) {
            ElMessage.success(res.msg);
            getProjectList();
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch(() => {});
      break;

    default:
      break;
  }
};

/**
 * @description: 获取字典
 */
async function getDicts() {
  await getDictsApi("erp_maintance_apply_approval_status").then((res) => {
    // 审核状态
    if (res.code === 200) {
      checkList.value = res.data;
    }
  });

  await getDictsApi("erp_construction_status").then((res) => {
    // 施工状态
    if (res.code === 200) {
      statusList.value = res.data;
    }
  });
  await getDictsApi("erp_work_order_type").then((res) => {
    // 工单类型
    if (res.code === 200) {
      typeList.value = res.data;
    }
  });
}

/**
 * @description: 序号
 * @param {Number} index 索引
 */
function indexMethod(index) {
  const currentPage = params.pageNum;
  const pageSize = params.pageSize;
  const truelndex = (currentPage - 1) * pageSize + index + 1;
  return truelndex;
}

onMounted(() => {
  getProjectList();
  getDicts();
});
</script>
<style>
@import "@/assets/styles/confirmBtn.css";
</style>
<style lang="scss" scoped>
.project {
  padding: 20px;
  width: 100%;
  height: 100%;
  .search-item {
    // margin-bottom: 20px;
  }
  .pagination {
    margin-top: 20px;
  }
}
</style>
